﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Teigha.DatabaseServices;
using Teigha.GraphicsInterface;
using Teigha.GraphicsSystem;
using Teigha.Runtime;

namespace Com.OpenCode.DwgConvert.Src.UI
{
    public partial class ViewerForm : Form
    {
        public ViewerForm()
        {
            InitializeComponent();
        }

        public string DwgFile { get; set; }

        public Database DwgDatabase { get; set; }

        public Device Device { get; set; }

        public LayoutHelperDevice LayoutHelper { get; set; }

        public Graphics PaintGraphics { get; set; }

        private void ViewerForm_Load(object sender, EventArgs e)
        {
            CreateDevice();
            DwgDatabase = new Database(false, false);
            DwgDatabase.ReadDwgFile(DwgFile, FileOpenMode.OpenForReadAndReadShare, false, "");
            CreateLayout();
        }

        private void ViewerForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (DwgDatabase != null)
            {
                DwgDatabase.Dispose();
                Device.Dispose();
                LayoutHelper.Dispose();
                PaintGraphics.Dispose();
            }
        }

        private void CreateDevice()
        {
            PaintGraphics = Graphics.FromHwnd(panel1.Handle);
            GsModule gsModule = (GsModule)SystemObjects.DynamicLinker.LoadModule("WinBitmap.txv", false, true);
            Device = gsModule.CreateDevice();
            Device.Properties.AtPut("WindowHWND", new RxVariant(panel1.Handle));
            Device.Properties.AtPut("WindowHDC", new RxVariant(PaintGraphics.GetHdc()));
            Device.Properties.AtPut("DoubleBufferEnabled", new RxVariant(true));
            Device.Properties.AtPut("EnableSoftwareHLR", new RxVariant(true));
            panel1.Resize += panel1_Resize;
        }

        private void CreateLayout()
        {
            ContextForDbDatabase contextForDbDatabase = new ContextForDbDatabase(DwgDatabase);
            contextForDbDatabase.UseGsModel = true;
            LayoutHelper = LayoutHelperDevice.SetupActiveLayoutViews(Device, contextForDbDatabase);
            LayoutHelper.UserGiContext = contextForDbDatabase;
            LayoutHelper.SetLogicalPalette(Device.DarkPalette);
            Rectangle rectangle = panel1.Bounds;
            rectangle.Offset(-panel1.Location.X, -panel1.Location.Y);
            LayoutHelper.OnSize(rectangle);
            panel1.Invalidate();
        }

        private void panel1_Resize(object sender, EventArgs e)
        {
            if (LayoutHelper == null)
            {
                return;
            }
            Rectangle rectangle = panel1.Bounds;
            rectangle.Offset(-panel1.Location.X, -panel1.Location.Y);
            LayoutHelper.OnSize(rectangle);
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            if (LayoutHelper == null)
            {
                return;
            }
            LayoutHelper.Update();
        }
    }
}
